#******************************************************************************
#******************************************************************************

import math

#******************************************************************************
#******************************************************************************

def thermal_resistance_from_shape_factor(shape_factor, 
                                         thermal_conductivity):
    """Computes the thermal resistance based on the conduction shape factor."""
    
    # Source: Cengel. Y, Heat Transfer: A Practical Approach, 2003, page 170
    
    # SI units:
    # shape factor: m
    # thermal conductivity: W/(m*K)
    # thermal resistance: K/W 
    
    return 1/(shape_factor*thermal_conductivity)

#******************************************************************************
#******************************************************************************

def shape_factor_buried_horizontal_isothermal_cylinder(diameter, 
                                                       length, 
                                                       depth):
    """Computes the shape factor for buried horizontal isothermal cylinders."""
    
    # Source: Cengel. Y, Heat Transfer: A Practical Approach, 2003, page 171
    
    # checks:
    # 1) length >> diameter
    # 2) depth >> 1.5*diameter
    
    if length <= diameter:
        
        raise Warning('The length should be much greater than the diameter.')
        
    if depth <= 1.5*diameter:
        
        raise Warning('The depth should exceed at least 1.5 diameters.')
    
    return 2*math.pi*length/math.log(4*depth/diameter)

#******************************************************************************
#******************************************************************************

def shape_factor_two_parallel_isothermal_cylinders(diameter_pipe1, 
                                                   diameter_pipe2, 
                                                   interpipe_distance,
                                                   length):
    """Computes the shape factor fo two parallel isothermal cylinders."""
    
    # Source: Cengel. Y, Heat Transfer: A Practical Approach, 2003, page 171
    
    # checks:
    # 1) length >> diameter1, diameter2
    # 2) length >> distance
    
    if length <= interpipe_distance:
        
        raise Warning(
            'The length should greatly exceed the pipe-to-pipe distance.')
        
    if length <= diameter_pipe1 or length <= diameter_pipe2:
        
        raise Warning(
            'The length should greatly exceed the diameters of both pipes.')
    
    return 2*math.pi*length/(
        math.acosh(
            (4*interpipe_distance**2-
             diameter_pipe1**2-
             diameter_pipe2**2)/(
                 2*diameter_pipe1*diameter_pipe2
                 )
            )
        )

#******************************************************************************
#******************************************************************************